<template>
    <div>
        <div>
            <div>
                <label for="">
                    账号：
                    <input type="text" v-model="name" />
                </label>
            </div>
            <div>
                <label for="">
                    密码：
                    <input type="text" v-model="password" />
                </label>
            </div>
            <button @click="login">登录</button>
            <button @click="toRegistered">去注册</button>
        </div>
    </div>
</template>

<script>
import network from '../network/index';
export default {
    data() {
        return {
            name: '',
            password: ''
        };
    },
    methods: {
        login() {
            if (!this.name) {
                alert('账号不能为空');
            } else if (!this.password) {
                alert('密码不能为空');
            } else {
                network({
                    url: '/login',
                    method: 'post',
                    data: {
                        name: this.name,
                        password: this.password
                    }
                }).then((res) => {
                    if (res.data.code === 200) {
                        // this.$store.commit('changeLogin', true);
                        // localStorage.setItem('name', this.name);
                        localStorage.setItem('name', this.name);
                        this.$router.push('/');
                    } else {
                        alert(res.data.data);
                    }
                });
            }
        },
        toRegistered() {
            this.$router.push('/registered');
        }
    }
};
</script>

<style></style>
